package com.example.myapplication.activity.base_activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.myapplication.R;

public class ButtonClickActivity extends AppCompatActivity implements View.OnClickListener{
    private  TextView tv_title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_button_click);
        tv_title = findViewById(R.id.tv_click_single);
        Button btn_click_single = findViewById(R.id.btn_click_single);
        Button btn_click_public =findViewById(R.id.btn_click_global);
        btn_click_public.setOnClickListener(this);
        btn_click_single.setOnClickListener(new MyViewClickListener(tv_title));
    }

    @Override
    public void onClick(View view) {
            if (view.getId()==R.id.btn_click_global){
                String desc = String.format("Button Clicked全局的: %s", System.currentTimeMillis());
                this.tv_title.setText(desc);
            }
    }

    /**
     * 内存泄漏这个事情 可能会由于没加static导致
     */
    static class MyViewClickListener implements View.OnClickListener {
        private final TextView tv_title;

        MyViewClickListener(TextView textView) {
            this.tv_title = textView;
        }

        @Override
        public void onClick(View v) {
            String desc = String.format("Button Clicked单独的: %s", System.currentTimeMillis());
           this.tv_title.setText(desc);
        }
    }
}